package cn.cs.service.impl;

import cn.cs.client.RestCsImageClient;
import cn.cs.client.RestCsProductClient;
import cn.cs.common.*;
import cn.cs.exception.ItemErrorCode;
import cn.cs.pojo.CsImage;
import cn.cs.pojo.CsProduct;
import cn.cs.service.ParentAndChildService;
import cn.cs.vo.SlideShowVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 亲子页服务接口实现
 */
@Component
public class ParentAndChildServiceImpl implements ParentAndChildService {
    private static final Logger logger = LoggerFactory.getLogger(ParentAndChildServiceImpl.class);
    //@Autowired
    @Resource
    private RestCsProductClient restCsProductClient;

    //@Autowired
    @Resource
    private RestCsImageClient restCsImageClient;


    //@Autowired
    @Resource
    private RedisUtils redisUtils;

    @Override
    public Dto<List<SlideShowVo>> querySlideShowPic(Long itemTypeId) throws Exception {
        SlideShowVo slideShowVo = null;
        Dto<List<SlideShowVo>> dto = null;
        List<CsProduct> csProductList = getItemList("itemType1Id", itemTypeId, 1, 5);
        List<SlideShowVo> dataList = new ArrayList<SlideShowVo>();
        for (CsProduct item : csProductList) {
            logger.info("[querySlideShowPic]" + "dmItemList size>>>>>>>>>>>>>>>"+csProductList.size());
            //获取对应的轮播图信息
            String imgUrl = getImageUrl(restCsImageClient, Long.parseLong(item.getId()), Constants.Image.ImageType.carousel, Constants.Image.ImageCategory.item);
            //封装返回数据
            slideShowVo = new SlideShowVo();
            BeanUtils.copyProperties(item, slideShowVo);
            slideShowVo.setImgUrl(imgUrl);
            dataList.add(slideShowVo);
        }
        dto = DtoUtil.returnDataSuccess(dataList);
        return dto;
    }


    /**
     * 获取列表信息
     *
     * @param paramName   过滤参数名称
     * @param paramValue  参数值
     * @param isRecommend 是否是热门推荐
     * @param limit       请求数目
     * @return
     * @throws Exception
     */
    public List<CsProduct> getItemList(String paramName, Object paramValue, Integer isRecommend, Integer limit) throws Exception {
        Map<String, Object> paramMapItem = new HashMap<String, Object>();
        paramMapItem.put(paramName, paramValue);
        paramMapItem.put("isRecommend", isRecommend);
        if (limit != 0) {
            paramMapItem.put("pageSize", limit);
            paramMapItem.put("beginPos", 0);
        }
        //根据分类查询对应的节目信息
        List<CsProduct> dmItemList = restCsProductClient.getCsProductListByMap(paramMapItem);
        if (EmptyUtils.isEmpty(dmItemList)) {
            throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        return dmItemList;
    }

    /**
     * 查询图片信息
     *
     * @param restCsImageClient
     * @param type
     * @param category
     * @return
     */
    public String getImageUrl(RestCsImageClient restCsImageClient, Long id, Integer type, Integer category) {
        //查询图片信息
        List<CsImage> dmImageList = null;
        //从reidis中获取图片信息
        String key = Constants.IMAGE_TOKEN_PREFIX + id + "_" + type + "_" + category;
        String imgUrl = (String) redisUtils.get(key);
        //如果redis中没有,则到数据库中获取
        if (EmptyUtils.isEmpty(imgUrl)) {
            dmImageList = restCsImageClient.queryCsImageList(id, type, category);
            imgUrl = dmImageList.get(0).getImgUrl();
            //同步图片到redis
            redisUtils.set(key, imgUrl);
        }
        return imgUrl;
    }

}
